<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/4/19/019
 * Time: 14:05
 */

namespace backend\models\forms;


use backend\models\BaseModel;
use common\models\game\FivepkDiamond;
use common\models\game\SeoidDiamond;
use common\services\Messenger;
use common\services\ToolService;
use yii\base\Model;
use yii\db\ActiveRecord;
use yii\widgets\ActiveForm;

class AgentDiamondForm extends Model
{
    public $agent;
    public $general_agent;
    public $diamond_up;
    public $diamond_down;
    public $comment = '充钻';
    public $agent_model;
    public $general_agent_model;
    public $agent_diamond;
    public $general_agent_diamond;

    public function rules()
    {
        return [
            [['agent','general_agent','comment'],'string','max'=>'255'],
            [['diamond_down','diamond_up'],'integer'],
            [['diamond_up','diamond_down'],'validateDiamond'],
            [['diamond_up'],'validateDiamondUp','on'=>'agent'],
            [['diamond_down'],'validateDiamondDown'],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'agent' => '代理商号',
            'general_agent' => '总代理商号',
            'diamond_up' => '上钻',
            'diamond_down' => '下钻',
            'comment' => '备注',
        ];
    }


    public function load($data, $formName = null)
    {
        $result = parent::load($data, $formName);

        $this->diamond_up = empty($this->diamond_up)?0:$this->diamond_up;
        $this->diamond_down = empty($this->diamond_down)?0:$this->diamond_down;
        $this->agent_model = SeoidDiamond::findOne(['seoid'=>$this->agent]);
        $this->agent_diamond = $this->agent_model->diamond;

        if($this->scenario == 'agent'){
            $this->general_agent_model = SeoidDiamond::findOne(['seoid'=>$this->general_agent]);
            $this->general_agent_diamond = $this->general_agent_model->diamond;
        }
        return $result;// TODO: Change the autogenerated stub
    }

    public function validateDiamond($attribute)
    {
        if(isset($this->diamond_down) && isset($this->diamond_up) && $this->diamond_down>0  && $this->diamond_up >0){
            $this->addError($attribute,'不能同时上下钻');
        }
        if(!isset($this->diamond_down) && !isset($this->diamond_up)){
            $this->addError($attribute,'不能同时空');
        }
        if($this->diamond_up <=0 && $this->diamond_down<=0){
            $this->addError($attribute,'不能同时小于等于0');
        }
    }

    public function validateDiamondUp($attribute)
    {
        if(!empty($this->diamond_up) && ($this->general_agent_diamond-$this->diamond_up)<0){
            $this->addError($attribute,'总代理钻石不足');
        }
    }

    public function validateDiamondDown($attribute)
    {
        if(!empty($this->diamond_down) && ($this->agent_diamond-$this->diamond_down)<0){
            $this->addError($attribute,'代理商钻石不足');
        }
    }

    public function updateAgentDiamond()
    {
        $validate = true;

        $this->agent_model->diamond = $this->agent_model->diamond+(int)$this->diamond_up-(int)$this->diamond_down;
        $validate = $validate && $this->agent_model->save();

        $data['FivepkDiamond'] = [
            'diamond_up' => (int)$this->diamond_up,
            'diamond_down' => (int)$this->diamond_down,
            'diamond_before' => $this->agent_diamond,
            'diamond_after' => $this->agent_model->diamond,
            'seoid' => $this->agent,
            'operator' => \Yii::$app->user->identity->uname,
            'diamond_type' => $this->comment,
            'operator_time' => time()*1000
        ];
        $model = new FivepkDiamond();
        $validate = $validate && $model -> load($data) && $model -> validate() && $model -> save();

        if(!empty($this->general_agent_model)){
            $this->general_agent_model->diamond = $this->general_agent_model->diamond-(int)$this->diamond_up+(int)$this->diamond_down;
            $validate = $validate && $this->general_agent_model->save();
        }

        $contents = new Messenger();
        if(!$validate){
            $contents -> status = 0;
            $error_str = null;
            foreach ($model->errors as $error){
                $error_str .= $error[0].',';
            }
            $contents -> message = substr($error_str,1);
        }

        return $contents;
    }

    public function getDiamondType()
    {
        if((empty($this->diamond_down) || $this->diamond_down<=0) && (!empty($this->diamond_up) && $this->diamond_up>0)){
            return '上钻'.$this->diamond_up;
        }elseif((empty($this->diamond_up) || $this->diamond_up<=0) && (!empty($this->diamond_down) && $this->diamond_down>0)){
            return '下钻'.$this->diamond_down;
        }
    }

}